﻿#pragma once

#include "CoreMinimal.h"
#include "IDetailsView.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Input/Reply.h"
#include "Engine/EngineTypes.h"
#include "IStructureDetailsView.h"
#include "Widgets/SCompoundWidget.h"
#include "Logging/LogMacros.h"
#include "SCombineTexture.generated.h"

DECLARE_LOG_CATEGORY_EXTERN(CombineTextureLog, Log, All);

UENUM(BlueprintType)
enum class EColorCompressChannel :uint8
{
	ECC_R UMETA(DisplayName = "R"),
	ECC_G UMETA(DisplayName = "G"),
	ECC_B UMETA(DisplayName = "B"),
	ECC_A UMETA(DisplayName = "A")
};


USTRUCT(BlueprintType)
struct COMBINETEXTURE_API FCombineSettingBase
{
	GENERATED_BODY()
public:
	FCombineSettingBase();

	void SaveSettings();
	FString GetOutFilter();

	//SourceFilter :源贴图所在的位置
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Filter", meta = (DisplayName = "源文件夹"))
		FDirectoryPath SourceFilter;
	//OutFilter :合并之后的贴图存储位置
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Filter", meta = (DisplayName = "输出文件夹"))
		FDirectoryPath OutFilter;

	//BaseColorKey : 在源文件夹中查找 基础颜色贴图 的关键词
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FileKey", meta = (DisplayName = "基础颜色贴图关键词"))
		FString BaseColorKey;
	//MetallicKey : 在源文件夹中查找 金属贴图 的关键词
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FileKey", meta = (DisplayName = "金属贴图关键词"))
		FString MetallicKey;
	//SpecularKey : 在源文件夹中查找 高光贴图 的关键词
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FileKey", meta = (DisplayName = "高光贴图关键词"))
		FString SpecularKey;
	//RoughnessKey : 在源文件夹中查找 粗糙贴图 的关键词
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FileKey", meta = (DisplayName = "粗糙贴图关键词"))
		FString RoughnessKey;
	//AmbientOcclusionKey : 在源文件夹中查找 AO贴图 的关键词
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FileKey", meta = (DisplayName = "AO贴图关键词"))
		FString AmbientOcclusionKey;

	//CompessChannel : 压缩效果图的哪个通道，同时也将使用这个通道中的数据去合并
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings", meta = (DisplayName = "压缩通道"))
		EColorCompressChannel CompressChannel;
	//bForceCombine  ：在没有找到相关效果图时，将继续强制合并。
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings", meta = (DisplayName = "强制合并"))
		bool bForceCombine;
	//bDeleteSourceFile  ：合并之后删除所有源文件
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings", meta = (DisplayName = "合并之后删除源文件"))
		bool bDeleteSourceFile;
};


class SCombineTexture : public SCompoundWidget
{

public:
	SLATE_BEGIN_ARGS(SCombineTexture)
	{}
	SLATE_END_ARGS()

public:
	/**
	* Construct the widget
	*
	* @param	InArgs			A declaration from which to construct the widget
	*/
	void Construct(const FArguments& InArgs);
private:
	void CreateExportFilterListView();

	TSharedPtr<IStructureDetailsView> SettingsView;
	TSharedPtr<FCombineSettingBase> CombineSettingBase;

	FReply CombineTexture() const;

	bool GetAllFilePathAtSourceFilter(FString& OutBaseColorPath, FString& OutMetallicPath, FString& OutSpecularPath, FString& OutRoughnessPath, FString& OutAmbientOcclusionPath) const;
	TArray<uint8> GetLocalTexture(const FString& _TexPath) const;
	TArray<uint8> CompressTexture(const TArray<uint8> SourceTexture, const int CombineLength, bool& succeed) const;
	void ExportTexture(const TArray<uint8>* uncompressedRGBA, const FString& FileName, const int& TextureLength) const;
	FString GetOutFileName(const FString& BaseColorPath) const;

};

